<?php

class WeiboHandler {

    private $template = array(
        'add' => 'http://weibo.com/aj/mblog/add?ajwvr=6&__rnd={rand_time}',
        'forward' => 'http://weibo.com/aj/v6/mblog/forward?ajwvr=6&domain=100505&__rnd={rand_time}'
    );

    /**
     * 发布微博
     */
    public function add($text, $uid) {
        
    }

    /**
     * 点赞操作
     */
    public function like() {
        
    }

    /**
     * 发布一条评论
     */
    public function comment() {
        
    }

    /**
     * 转发一条微博
     */
    public function forward($data, $params) {
        $default = array(
            'pic_src' => '',
            'pic_id' => '',
            'appkey' => '',
            'mid' => '',
            'style_type' => 1,
            'mark' => '',
            'reason' => '',
            'location' => 'page_100505_single_weibo',
            'pdetail' => '',
            'module' => '',
            'page_module_id' => '',
            'refer_sort' => '',
            'is_comment_base' => 0,
            'is_comment' => 0,
            'rank' => 1,
            'rankid' => '',
            '_t' => 0
        );
        $data = array_merge($default, $data);
        ignore_user_abort();
        set_time_limit(0);
        $weibo = new Weibo;
        if ($params['is_quick']) {
            foreach ($params['contents'] as $k => $v) {
                foreach ($params['account_ids'] as $key => $value) {
                    $weibo = new Weibo;
                    $data['reason'] = $v;
                    $rand_time = time() . rand(100, 999);
                    $url = str_replace('{rand_time}', $rand_time, $this->template['forward']);
                    $filecache = new FileCache;
                    $cookie = $filecache->get('info' . $value);
                    $headers = array(
                        'Host' => 'weibo.com',
                        'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
                        'Accept' => '*/*',
                        'Accept-Language' => 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                        'Accept-Encoding' => 'gzip, deflate',
                        'X-Requested-With'=> 'XMLHttpRequest',
                        'Referer' => 'http://weibo.com/u/' . $value . '/home?wvr=5',
                        'Connection' => 'keep-alive'
                    );
                    var_dump($weibo->request_url($url, $data, $cookie, $headers));
                    die;
                }
                $time = rand($params['rate'][0], $params['rate'][1]);
                sleep($time);
            }
        }
    }

    /**
     * 发布超话帖子
     */
    public function proxy() {
        
    }

    /**
     * 存至数据库
     * @param type $from
     * 数据库结构
     * id[id]  from[来源]    uid[哪个用户]   data[要发送的数据]  url[待请求的url] create_time[创建时间] post_time[发布时间]
     */
    public function save($from) {
        
    }

}
